package com.iseastar.guojiang.newcabinet;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.dianxiaosan.model.Const;
import com.iseastar.guojiang.BaseFragment;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.dialog.DialogMgr;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.ServiceTaskBean;
import com.iseastar.guojiang.model.TaskStageBean;
import com.shouhuobao.station.R;
import com.shouhuobao.take.utils.AppTab;

import droid.frame.ui.utils.Utils;
import droid.frame.view.ViewPagerExt;

/**
 * Created by joker on 2017/12/28.
 * 同城任务阶段和任务详情主界面
 */
public class SameCityTaskMainActivity extends BaseActivity2 implements View.OnClickListener {

    public BaseFragment mFragment1, mFragment2;
    private TextView mRightTV;
    private ServiceTaskBean serviceTaskBean = null;
    private TaskStageBean stageBean = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        serviceTaskBean = (ServiceTaskBean) getIntent().getSerializableExtra("item");
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_task_detail_main);
        super.findViewById();
        findViewById(R.id.title_left_image).setOnClickListener(this);
        mRightTV = (TextView) findViewById(R.id.title_right_text);
        mRightTV.setOnClickListener(this);
        TextView[] mTags = new TextView[2];
        mTags[0] = (TextView) findViewById(R.id.task_tab1);
        mTags[1] = (TextView) findViewById(R.id.task_tab2);
        initFragment();
        ViewPagerExt mViewPager = (ViewPagerExt) findViewById(R.id.viewpager);
        ImageView mImgLine = (ImageView) findViewById(R.id.task_line_img);
        mViewPager.setSupportScroll(false);
        mViewPager.setFragmentItems(getSupportFragmentManager(), mFragment1, mFragment2);
        mViewPager.setSupportScroll(true);
        AppTab appTab = new AppTab(getContext(), 0);
        appTab.setWidthMargin(Utils.dpToPx(150, getResources()));
        appTab.setTextColor(getResources().getColor(R.color.white), Color.parseColor("#D1D1D6"));
        appTab.initPosition(mViewPager, mImgLine, mTags);

        mViewPager.setCurrentItem(0, true);

    }

    private void initFragment() {
        mFragment1 = new SameCityTaskStageFragment();
        Bundle bundle1 = new Bundle();
        bundle1.putInt("type", 0);
        bundle1.putSerializable("item", serviceTaskBean);
        mFragment1.setArguments(bundle1);

        mFragment2 = new SameCityTaskDetailFragment();
        Bundle bundle2 = new Bundle();
        bundle2.putInt("type", 1);
        bundle2.putSerializable("item", serviceTaskBean);
        mFragment2.setArguments(bundle2);
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            //返回上一页
            case R.id.title_left_image:
                finish();
                break;
            //任务异常
            case R.id.title_right_text:
                if (null == serviceTaskBean && null == stageBean) {
                    return;
                }
                //显示任务异常
                if (Const.CourierTaskFinishStatus.contractSender == stageBean.getStepType() || Const.CourierTaskFinishStatus.CityAcceptanceSendCode == stageBean.getStepType()) {
                    intent = new Intent(getContext(), TaskExceptionActivity.class);
                    intent.putExtra("item", serviceTaskBean);
                    startActivityForResult(intent, 1100);
                } else {
                    DialogMgr.showPhoneDialog(getContext(), null);
                }
                break;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            //同城完成揽收包裹返回,如果成功销毁当前的类
            case MsgID.courier_samecity_task_complete:
                final ReqResult<ServiceTaskBean> sameCityResult = JSON.parser(msg.obj, ServiceTaskBean.class);
                if (checkLoginStatus(sameCityResult)) {
                    finish();
                }
                return false;
            //获取当前任务阶段操作步骤数据返回
            case MsgID.courier_task_step_detail:
                cancelLoadingDialog();
                final ReqResult<TaskStageBean> reqResult = JSON.parser(msg.obj, TaskStageBean.class);
                if (checkLoginStatus(reqResult)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            stageBean = reqResult.getResult();
                            if (stageBean == null) {
                                return;
                            }
                            //显示任务异常
                            if (Const.CourierTaskFinishStatus.contractSender == stageBean.getStepType() || Const.CourierTaskFinishStatus.CityAcceptanceSendCode == stageBean.getStepType()) {
                                mRightTV.setText("任务异常");
                            } else {//显示联系客服
                                mRightTV.setText("联系客服");
                            }
                        }
                    });
                } else {
                    showToast(reqResult.getMessage());
                }
                return false;
            default:
                return super.handleMessage(msg);
        }
    }
}
